wx.cloud.init()
Page({
  
  data: {
    listData: [],
    currentIdx: '',
    hiddenNametop: false,
    hiddenName: true,
    formData: {
      stu_number: '',
      password: '',
      phone: '',
      address: '',
      statue: ''
    }
  },

  onLoad: function (options) {
    const db = wx.cloud.database()
    db.collection('data_stu').get({
      success: res => {
      
        this.setData({
          listData: res.data
        })
      }  
    })
    
  },
  click: function (e) {
  
    this.setData({
      formData: e.target.dataset.item,
      currentIdx: e.target.dataset.index,
      hiddenName: !this.data.hiddenName,
      hiddenNametop: !this.data.hiddenNametop,
    })
  },
  user: function () {
    wx.navigateTo({
      url: '/pages/studentInformation/studentInformation'
    })
  },
  librarymanagement: function () {
    wx.navigateTo({
      url: '/pages/studentInformation/studentInformation'
    })
  },
  formSubmit: function (e) {
    const stu_number = e.detail.value.stu_number
    //拿到的数据将其与数据库中的学号进行匹配，然后进行数据库的更新，弹窗出来，显示成功
    const newData = this.data.listData
    
    newData[this.data.currentIdx].stu_number = e.detail.value.stu_number
    newData[this.data.currentIdx].password = e.detail.value.password
    newData[this.data.currentIdx].phone = e.detail.value.phone
    newData[this.data.currentIdx].address = e.detail.value.address
    newData[this.data.currentIdx].statue = e.detail.value.statue
  
    this.setData({
      listData: newData,
      hiddenName: !this.data.hiddenName,
      hiddenNametop: !this.data.hiddenNametop
    })
    const db = wx.cloud.database()
  
   db.collection('data_stu').where({stu_number}).update({
    data: {
      ...e.detail.value
    },
    success(data){
      console.log(data);
    }
  })
  },
  //删除
  del:function(e){
    const db = wx.cloud.database()
    const newData = e.target.dataset.item
    const index =e.target.dataset.index
    const suoyin = newData._id
   wx.showModal({
    title: '提示！',
    content: '确认删除',
    showCancel: false,
    success(res) {
    
      db.collection('data_stu').where({
        _id:suoyin
      }).remove()
      wx.navigateTo({
        url: '/pages/administrators/administrators'
      })
    }
  })
  },
})